package com.woniuxy.carlife.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.carlife.Param.StoreParam;
import com.woniuxy.carlife.Param.StoreUserParam;
import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.Result;
import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.StatusCode;
import com.woniuxy.carlife.carlifecommonsweb.util.RandomCode;
import com.woniuxy.carlife.domain.Store;
import com.woniuxy.carlife.domain.StoreUser;
import com.woniuxy.carlife.domain.Tags;
import com.woniuxy.carlife.mapper.StoreMapper;
import com.woniuxy.carlife.mapper.StoreUserMapper;
import com.woniuxy.carlife.mapper.TagsMapper;
import com.woniuxy.carlife.service.StoreService;
import com.woniuxy.carlife.service.StoreUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.carlife.util.MinioTemplate;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: StoreUserServiceImpl
 *@Description: 服务实现类
 * @author: yanzhongliang     
 * @date: 2021-04-05
 */

@Service
public class StoreUserServiceImpl extends ServiceImpl<StoreUserMapper, StoreUser> implements StoreUserService {
    @Resource
    private RedisTemplate redisTemplate;
    //    引入门店账号数据层
    @Resource
    private StoreUserService storeUserService;
//    引入门店账号dao层
    @Resource
    private StoreUserMapper storeUserMapper;
    //    引入门店dao层
    @Resource
    private StoreMapper storeMapper;
    //引入门店服务标签dao层
    @Resource
    private TagsMapper tagsMapper;
    //    引入门店数据层
    @Resource
    private StoreService storeService;




    /**
     * 功能描述〈通过传入门店营业执照，店铺名，店主名，门店服务标签集合（[tagsName]），门店id，门店手机号，精度，纬度，门店地址，门店账号，密码〉
     * @author: yanzhongliang
     * @param:  [StoreParam]
     * @return: massage
     * @Date 2021/4/7 11:15
     * @throws
     */

    @Override
    public int setStoreUser(StoreParam storeParam) {
        if (!ObjectUtils.isEmpty(storeParam)){


        //      判断账号是否已被注册
        QueryWrapper<StoreUser> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("store_user_account",storeParam.getStoreUserAccount());
        //      判断手机号是否已被注册
        QueryWrapper<Store> queryWrapper2=new QueryWrapper<>();
        queryWrapper2.eq("store_phone",storeParam.getStorePhone());

        Store store=storeService.getOne(queryWrapper2);
        StoreUser storeUser=storeUserService.getOne(queryWrapper1);

        //如果手机号和账号没被注册则执行密码加密和注册
        if (ObjectUtils.isEmpty(storeUser)&&ObjectUtils.isEmpty(store)){
            String salt= RandomCode.getCode(8);
            Md5Hash md5Hash=new Md5Hash(storeParam.getStoreUserPassword(),salt,2048);
                StoreUser storeUser1=new StoreUser();
            //保存加密的门店密码
            storeUser1.setStoreUserPassword(md5Hash.toHex());
            //保存门店账号
            storeUser1.setStoreUserAccount(storeParam.getStoreUserAccount());
            //保存随盐
            storeUser1.setSalt(salt);
            //创建一个门店对象
            Store store1=new Store();
            //保存店铺名
            store1.setStoreName(storeParam.getStoreName());
            //保存营业执照
            store1.setStoreBusiness(storeParam.getStoreBusiness());
            //保存店铺图片
            store1.setStoreImage(storeParam.getStoreInfoImage());
            //保存门店手机号
            store1.setStorePhone(storeParam.getStorePhone());
            //保存精度
            store1.setStorePrecision(storeParam.getStorePrecision());
            //保存纬度
            store1.setStoreLatitude(storeParam.getStoreLatitude());
            //保存店长名
            store1.setStoreShopkeeperName(storeParam.getStoreShopkeeperName());
            //保存门店地址
            store1.setStoreAddress(storeParam.getStoreAddress());
            //保存创建时间
            store1.setStoreCreateTime(new Timestamp(new Date().getTime()));
            //1.注册门店营业执照，图片，名字...到Store数据库
            storeMapper.insert(store1);
            //获取到门店ID
            Integer storeid=store1.getStoreId();
            //把注册的门店ID添加到门店账户管理表中
            storeUser1.setStoreId(storeid);
            //2.注册账号到StoreUser数据库
            storeUserMapper.insert(storeUser1);
            //添加门店服务标签
            Tags tags=new Tags();
            //存入门店ID
            tags.setStoreId(storeid);
            //存入创建时间
            tags.setTagsCreateTime(new Timestamp(new Date().getTime()));
            //把传入的门店服务标签集合转成json字符串
            String tagsName=JSON.toJSONString(storeParam.getTagsName());
            //存入门店服务标签
            tags.setTagsName(tagsName);
            //添加tags到Tags数据库
            tagsMapper.insert(tags);

            return 1;
        }else {
            return 0;
        }
        }
        return 0;
    }

    /**
     * 功能描述〈根据店铺账号密码登陆〉
     *
     * @throws
     * @author: yanzhongliang
     * @param: storeUserParam
     * @return: massage
     * @Date 2021/4/7 9:22
     */

    @Override
    public int getStoreUser(StoreUserParam storeUserParam) {
//        UsernamePasswordToken token = new UsernamePasswordToken(storeUserParam.getStoreUserAccount(),
//                storeUserParam.getStoreUserPassword(), storeUserParam.getChecked());
        //判断输入是否为空
        if (!ObjectUtils.isEmpty(storeUserParam)) {
            //根据门店账号查询密码和盐
            QueryWrapper<StoreUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("store_user_account", storeUserParam.getStoreUserAccount());
            StoreUser storeUser = storeUserMapper.selectOne(queryWrapper);
            //判断该账号是否存在
            if(!ObjectUtils.isEmpty(storeUserParam)) {
                //将传入的密码进行加密
                Md5Hash md5Hash = new Md5Hash(storeUserParam.getStoreUserPassword(), storeUser.getSalt(), 2048);
                String loginPassword = md5Hash.toHex();
                //将加密后的密码和数据库中的密码进行对比,登录成功返回0，失败返回1
                if (loginPassword.equals(storeUser.getStoreUserPassword())) {
                    return 0;
                } else {
                    return 1;
                }
            }else {
                return 2;
            }
        }
            return 2;
    }
}
